package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import model.SearchAccount;

public class SearchAccountDAO  implements DAO{

	final static Logger logger = Logger.getLogger(SearchAccountDAO.class);

    private final DataSource dataSource;

    public SearchAccountDAO() {
    	
    	dataSource = new DataSource();
    }
	
	public ArrayList<SearchAccount> readByKey(String key) {
		
		try {
			
			String sql = "SELECT * FROM planAccount where subgroupaccount=? ";
			
		    PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
		    
		    ArrayList<SearchAccount> listaContas = new ArrayList<SearchAccount>();
		    
		    ps.setString(1, key);
		    ResultSet rs = ps.executeQuery();
		    
		    while (rs.next()) {
		    	
		    	SearchAccount sa = new SearchAccount();
		    	
		    	sa.setId(rs.getString("planAccount.id"));
		    	sa.setDescricao(rs.getString("planAccount.descricao"));
		    	sa.setPlanaccount(rs.getString("planAccount.groupaccount"));
		    	sa.setSubgroupaccount(rs.getString("planAccount.subgroupaccount"));
		    	sa.setDescextendida(rs.getString("planAccount.desc_extendida"));
		    	
		    
		    	listaContas.add(sa);
		    	
		    	
		    	
		    }
		    return listaContas;
			
			
		} catch (Exception e) {
			
			logger.error("GROUPACCOUNT.READBYKEY - " , e);
	            return null;
			
			
		}
		
		
	}
    
    public ArrayList<SearchAccount> readKey(String key) {
   	
    	try {
			
			String sql = "SELECT * FROM planAccount where id=? ";
		    PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
		    ArrayList<SearchAccount> listaContas = new ArrayList<SearchAccount>();
		    ps.setString(1, key);
		    ResultSet rs = ps.executeQuery();
		    
		    	while (rs.next()) {
		    	 SearchAccount sa = new SearchAccount();
		    	 sa.setId(rs.getString("planAccount.id"));
			    	sa.setDescricao(rs.getString("planAccount.descricao"));
			    	sa.setPlanaccount(rs.getString("planAccount.groupaccount"));
			    	sa.setSubgroupaccount(rs.getString("planAccount.subgroupaccount"));
			    	sa.setDescextendida(rs.getString("planAccount.desc_extendida"));
		    	
		    
		    	listaContas.add(sa);
		    	
		    	
		    	
		    }
		    return listaContas;
			
		} catch (Exception e) {
			
			logger.error("GROUPACCOUNT.READKEY " ,e);
	            
			
			
		}
    	return null;
    	
    }
    
    
	@Override
	public void create(Object o) {
		// TODO Auto-generated method stub
		
	} 

	@Override
	public void delete(Object o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Object read(Object o) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void closeDAO() {
		dataSource.closeConnection();
	}

}
